﻿using OpenCvSharp;

using System;
using System.IO;
using System.Linq;

using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    /// <summary>
    /// 文件写入
    /// </summary>
    [Filter("文件写入",FilterType.OS)]
    public class OsImageWrite : FilterBase
    {
        /// <summary>
        /// 文件名
        /// </summary>
        [InputParameter("文件名")]
        public string Name { get; set; } = "Export_{0}.jpg";
        /// <summary>
        /// 是否自增
        /// </summary>
        [InputParameter("是否自增")]
        public bool AutoIncrement { get; set; } = true;
        /// <summary>
        /// 文件夹
        /// </summary>
        [InputParameter("文件夹")]
        public string FileDirectory { get; set; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"Images");
        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            string rand = string.Empty;
            if (AutoIncrement)
            {
                rand = DateTime.Now.ToBinary().ToString();
                rand= string.Format(Name, rand);
            }
            
            var path = Path.Combine(FileDirectory, rand);
            if (string.IsNullOrEmpty(Name))
            {
                return ResultData<Mat>.Reject(TextResource.bad_file_name,null);
            }
            else
            {
                if (!Directory.Exists(FileDirectory))
                {
                    Directory.CreateDirectory(FileDirectory);
                }
               var res=  mat.SaveImage(path);
                return   ResultData<Mat>.From(res, s => s ==true,mat);
            }
        }
    }
}
